package dd.prog2.musterExercises.ws12set13_4_mvc.gui;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractButton;

/**
 * Translates button events into model operations.
 * 
 * @author Ruediger Lunde
 */
public class ConnectFourController implements ActionListener {

	private ConnectFourView view;
	private ConnectFourModel model;

	public ConnectFourController(ConnectFourModel model) {
		this.model = model;
	}

	public void setView(ConnectFourView view) {
		this.view = view;
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() instanceof AbstractButton) {
			AbstractButton button = (AbstractButton) e.getSource();
			String cmd = button.getActionCommand();
			if (cmd == ConnectFourView.CLEAR_CMD)
				model.initGame();
			else if (cmd == ConnectFourView.PROPOSE_MOVE_CMD)
				model.proposeMove((view.timeCombo.getSelectedIndex() + 1) * 5,
						view.strategyCombo.getSelectedIndex() == 1);
			else if (e.getSource() instanceof ConnectFourView.GridElement) {
				ConnectFourView.GridElement el = (ConnectFourView.GridElement) e
						.getSource();
				model.makeMove(el.col);
			}
		}
	}
}
